package com.lcc.controller;

import com.lcc.anno.Log;
import com.lcc.pojo.Emp;
import com.lcc.pojo.PageBean;
import com.lcc.pojo.Result;
import com.lcc.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 员工管理Controller
 */
//lombok提供的记录日志注解，直接调用log就能使用。
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

    //面向接口开发
    @Autowired
    private EmpService empService;

    //条件查询员工
    @GetMapping      //@RequestParam(defaultValue = "1") 设置默认值
    public Result selectByPage(
            @RequestParam(defaultValue = "1") Integer page,  //注意这里的参数名称一定要和文档中的保持一致，否则肯传输不了数据
            @RequestParam(defaultValue = "5") Integer pageSize,
            String name, Short gender,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,//@DateTimeFormat指定日期格式
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ) {
        log.info("查询数据：第{}页，{}条数据,{},{},{},{}", page, pageSize, name, gender, begin, end);
        PageBean pageBean = empService.selectByPage(page, pageSize, name, gender, begin, end);
        return Result.success(pageBean);
    }

    //批量删除员工
    @Log
    @DeleteMapping("/{ids}")
    public Result deleteEmp(@PathVariable List<Integer> ids) {
        empService.deleteEmp(ids);
        return Result.success();
    }

    //新增员工
    @Log
    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("新增员工数据：{}", emp);
        empService.save(emp);
        return Result.success();
    }

    //根据ID查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询：{}",id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    //修改员工信息,json类型封装到实体需要RequestBody
    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工信息:{}", emp);
        empService.update(emp);
        return Result.success();
    }

}
